<template>
  <div>
    <el-dialog
      :title="eject.isadd == true ? '轮播图添加' : '轮播图修改'"
      :visible.sync="eject.isshow"
      @closed="guanbi"
    >
      <el-form :model="form">
        {{ form }}
      

        <el-form-item label="标题" label-width="120px" :rules="rules">
          <el-input
            v-model="form.title"
            autocomplete="off"
            required
          ></el-input>
        </el-form-item>

        <el-form-item label="图片" label-width="120px" :rules="rules">
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inaactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="add()" v-if="eject.isadd"
          >添 加</el-button
        >
        <el-button type="primary" @click="update" v-else>编 辑</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { IndexRouter } from "../../../router/index.js";
import {
  reqcatelist,
  reqbanneradd,
  reqbannerone,
  reqbanneredit,
} from "../../../request/api";
import { successAlert } from "../../../utils/alert";
import { mapActions } from "vuex";
export default {
  props: ["eject"],
  data() {
    return {
      form: {
      
        title: "",
        img: null,
        status: 1,
      },
      imageUrl: "",
      name: [],
      icons: [
        "el-icon-s-tools",
        "el-icon-user-solid",
        "el-icon-s-goods",
        "el-icon-help",
        "el-icon-s-operation",
      ],
      IndexRouter,
      rules: [
        {
          required: true,
        },
      ],
    };
  },
  created() {},
  mounted() {
    reqcatelist().then((res) => {
      this.name = res.data.list;
      console.log(res);
    });
  },
  computed: {},
  methods: {

    handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg/png/gif';
        const isLt2M = file.size / 1024 / 1024 < 2;

        // if (!isJPG) {
        //   this.$message.error('上传头像图片只能是 JPG 格式!');
        // }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        console.log(file);
        this.form.img =  file
        this.imageUrl = URL.createObjectURL(file)
        return isJPG && isLt2M;
      },
    ...mapActions({
      reqlist: "banner/reqlist",
    }),

    guanbi() {
      !this.eject.isadd && this.empty();
    },
    close() {
      this.$emit("close");
    },
    // 封装一个清空方法
    empty() {
      this.form = {
        
         title: "",
        img: null,
        status: 1,
      };
      this.imageUrl = null
    },
    //点击添加按钮
    add() {
      console.log(1);
      // 发起请求
      reqbanneradd(this.form).then((res) => {
        // 添加成功
        if (res.data.code == 200) {
          // 弹出成功
          successAlert(res.data.msg);
          //清空输入框
          this.empty();
          // 弹框消失
          this.eject.isshow = false;
          // 重新列表请求
          // this.$emit("init");
          this.reqlist();
        }
      });
    },
    update() {
      // 请求接口
      reqbanneredit(this.form).then((res) => {
        console.log(res);
        if (res.data.code == 200) {
          // 弹出成功提示框
          successAlert(res.data.msg);
          // 清空输入框
          this.empty();
          // 关闭弹窗
          this.eject.isshow = false;
          // 通知父组件更新列表
          this.reqlist();
        }
      });
    },
    changepid() {
      this.form.type = this.form.pid == 0 ? 1 : 2;
    },
    // 获取一条信息方法，父组件通过ref调用
    getone(id) {
      reqbannerone({ id: id }).then((res) => {
        this.form = res.data.list;
        //********************************* */

        this.form.id = id;
        this.imageUrl = this.$pre + this.form.img
      });
    },
  },
};
</script>

<style scoped  lang='less'>

 /deep/  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
   .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  
</style>
